﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Windows.Forms;
using System.Xml.Serialization;

// Required libraries to reference:
// - SAS: Integrated Object Model (IOM)
// - SASWorkspaceManager

namespace SasPlugIn.SASCli
{
    /// <summary>
    /// Config info for SAS System’s Integrated Object Model (IOM)
    /// </summary>
    public class IomProperties
    {
        #region Properties

        public string ServerDefName { get; set; }
        public short ServerPort { get; set; }
        public string DnsName { get; set; }
        public string WorkspaceName { get; set; }
        public string TablePrefix { get; set; }
        public string LibPath { get; set; }
        public string LoginName { get; set; }
        public string CryptedPwd { get; set; }

        #endregion

        #region C'tors

        /// <summary> Default C'tor </summary>
        public IomProperties()
        {
            Init();
        }

        public void Init()
        {
            ServerDefName = "OFSServer";
            ServerPort = 25271;
            DnsName = "sun53.bfs.admin.ch";
            WorkspaceName = "SMS_DEV";
            LoginName = "u4685";
            CryptedPwd = "";
            TablePrefix = "Meta";
            LibPath = "/app/home/bfs01/u4685/projet/Test/Phil/";
        }


        #endregion

        #region Serialization

        /// <summary>
        /// Update instance of this class according to XML doc.
        /// </summary>
        /// <param name="reader"> a text reader instance. </param>
        public void XmlRead(string filepath)
        {
            TextReader reader = null;
            IomProperties iom = null;
            try
            {
                reader = new StreamReader(filepath);

                XmlSerializer xmlSer = new XmlSerializer(this.GetType());
                iom = xmlSer.Deserialize(reader) as IomProperties;

                // now update ourself
                this.DnsName = iom.DnsName;
                this.CryptedPwd = iom.CryptedPwd;
                this.LoginName = iom.LoginName;
                this.ServerDefName = iom.ServerDefName;
                this.ServerPort = iom.ServerPort;
                this.WorkspaceName = iom.WorkspaceName;
                this.TablePrefix = iom.TablePrefix;
                this.LibPath = iom.LibPath;
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Unable to read. Invalid XML document", ex);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }
        }

        /// <summary>
        /// Xml serialization in specified filename
        /// </summary>
        /// <param name="xmlFile"> filename to create</param>
        public virtual void XmlWrite(string xmlFile)
        {
            StreamWriter sw = new StreamWriter(xmlFile);
            XmlWrite(sw);
        }

        /// <summary> XML Persistence using writer. </summary>
        /// <param name="tw"> an object derived from TextWriter</param>
        public virtual void XmlWrite(TextWriter tw)
        {
            try
            {
                XmlSerializer xmlSer = new XmlSerializer(this.GetType());
                xmlSer.Serialize(tw, this);
            }
            finally
            {
                tw.Close();
            }
        }
        #endregion

        #region factory loader

        /// <summary>Factory loader for properties default</summary>
        /// <returns>Instance of properties</returns>
        public static IomProperties LoadDefault()
        {
            // fetch default serialized
            IomProperties Properties = new IomProperties();

            // Fetch config if in app dir
            string ConfigBasePath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
            ConfigBasePath += "\\SasClient.cfg";
            if (System.IO.File.Exists(ConfigBasePath))
                Properties.XmlRead(ConfigBasePath);
            return Properties;
        }

        #endregion

    }
}
